//
//  PPCalendarDayCell.swift
//  PPKit
//
//  Created by xiaopin on 2021/12/5.
//  Copyright © 2021 sowell.com. All rights reserved.
//

import UIKit

class PPCalendarHeaderView: XPBaseHeaderFooterView {
    let titleLabel = UILabel.init(text: "yyyy年MM月", textColor: pp_color(.C222222), fontSize: 15, weight: .bold, align: .left)
    
    override func createUI() {
        contentView.backgroundColor = pp_color(.CF7F7F7)
        titleLabel.beAddInto(contentView).snp.makeConstraints { m in
            m.left.equalTo(15.scale)
            m.centerY.equalToSuperview()
        }
    }
}

class PPCalendarDayCell: XPBaseTableViewCell {
    
    /// 选中：返回yyyy-MM-dd格式值
    var selectedClosure:((Date, String)->())?
    
    var modelObj:PPCalendarMonthModel?
    
    override func createUI() {
        
    }
    
    //加载每月的日期数据
    func configureData(_ month:PPCalendarMonthModel){
        modelObj = month
        
        if month.days.count == 0 {return}
        contentView.subviews.forEach({$0.removeFromSuperview()})
        
        let itemWidth:CGFloat = pp_screen_width / 7.0
        let itemSize:CGFloat = 40.0
        var rows = 0
        for (i,day) in month.days.enumerated() {
            if i == 0{
                rows = 1
                continue
            }
            if day.weekday == 1 {
                rows += 1
            }
        }
        
        let nowDate = Date().toString(.style1)
        var index = 0
        outerLoop:for row in 0..<rows {
            for column in 0..<7 {
                let day = month.days[index]

                if column != day.weekday - 1 {continue}
                
                let bgColor = day.dateValue == nowDate ? pp_color(.C159F48) : pp_color(.CFFFFFF)
                let fontColor = day.dateValue == nowDate ? .white : pp_color(.C222222)
                let subfontColor = day.dateValue == nowDate ? UIColor.hexColor("#B7D1FF") : pp_color(.C999999)
                let text = day.dateValue == nowDate ? "MT_Date_Today".localized : day.value.str
                
                let itemView = UIView.init(frame: pp_frame(column.f*itemWidth, row.f*itemSize + row.f*1 + 5, itemWidth, itemSize))
                let dayView = UIView.init(frame: pp_frame((itemWidth-itemSize)/2, 0, itemSize, itemSize), bgColor: bgColor, cornerRadius: 4)
                let label = UILabel.init(frame: pp_frame(0, 2, itemSize, 20), text: text, textColor: fontColor, fontSize: 15, weight: .bold, align: .center)
                let subLabel = UILabel.init(frame: pp_frame(0, 22, itemSize, 16), text: day.text, textColor: subfontColor, fontSize: 10, weight: .regular, align: .center)
                let button = UIButton.init(frame: dayView.bounds, tag: 1000+index, target: self, action: #selector(dayAction(_:)))
                
                if day.text.isEmpty {
                    label.height = 38
                    subLabel.isHidden = true
                }
                
                if day.dateValue == nowDate {
                    label.font = .systemFont(ofSize: 11, weight: .bold)
                }
                
                contentView.addSubview(itemView)
                itemView.addSubview(dayView)
                dayView.addSubview(label)
                dayView.addSubview(subLabel)
                dayView.addSubview(button)
                
                index += 1
                if index == month.days.count {
                    break outerLoop
                }
            }
        }
        
    }
    
    //点击每一个日期事件
    @objc func dayAction(_ sender:UIButton){
        let index = sender.tag - 1000
        if let date = modelObj?.days[index].date, let dateValue = modelObj?.days[index].dateValue {
            selectedClosure?(date,dateValue)
        }
    }
}
